using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserContactRequestDA {

        private const string SP_CountByRequestReceiverUserKey = "UserContactRequest_CountByRequestReceiverUserKey";
		private const string SP_Delete = "UserContactRequest_Delete";
		private const string SP_GetByRequestReceiverUserKeyWithPaging = "UserContactRequest_GetByRequestReceiverUserKeyWithPaging";
        private const string SP_GetByRequestSenderUserKeyRequestReceiverUserKey = "UserContactRequest_GetByRequestSenderUserKeyRequestReceiverUserKey";
		private const string SP_Insert = "UserContactRequest_Insert";

		public static UserContactRequestEnt CreateFromReader(IDataReader reader) {
			UserContactRequestEnt item = new UserContactRequestEnt();
            item.RequestSenderUserKey = ConvertUtils.ToString(reader["RequestSenderUserKey"]);
			item.RequestReceiverUserKey = ConvertUtils.ToString(reader["RequestReceiverUserKey"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
            item.SenderName = ConvertUtils.ToString(reader["SenderName"]);
            item.SenderGender = (EUserGender)ConvertUtils.ToByte(reader["SenderGender"]);
            item.SenderBirthDate = ConvertUtils.ToDateTime(reader["SenderBirthDate"]);
            item.SenderCityID = ConvertUtils.ToShort(reader["SenderCityID"]);
            item.SenderCity = ConvertUtils.ToString(reader["SenderCity"]);
            item.SenderCountryID = ConvertUtils.ToShort(reader["SenderCountryID"]);
            item.SenderCountry = ConvertUtils.ToString(reader["SenderCountry"]);
            item.SenderEducationID = ConvertUtils.ToShort(reader["SenderEducationID"]);
            item.SenderEducation = ConvertUtils.ToString(reader["SenderEducation"]);
            item.SenderJobID = ConvertUtils.ToShort(reader["SenderJobID"]);
            item.SenderJob = ConvertUtils.ToString(reader["SenderJob"]);
            item.SenderImageCount = ConvertUtils.ToInt(reader["SenderImageCount"]);
            item.SenderBlast = ConvertUtils.ToString(reader["SenderBlast"]);
            item.SenderAvatarFileName = ConvertUtils.ToString(reader["SenderAvatarFileName"]);
            item.SenderAvatarImageStoreID = ConvertUtils.ToInt(reader["SenderAvatarImageStoreID"]);
            item.SenderAvatarStatus = (EUserAvatarStatus)ConvertUtils.ToByte(reader["SenderAvatarStatus"]);
            return item;
		}

        public static int CountByRequestReceiverUserKey(string requestReceiverUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = 0;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CountByRequestReceiverUserKey)) {
                db.AddParameter(cmd, "RequestReceiverUserKey", requestReceiverUserKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = ConvertUtils.ToInt(reader["Count"]);
                    }
                }
            }
            return result;
        }

        public static int Delete(string requestSenderUserKey, string requestReceiverUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Delete)) {
                db.AddParameter(cmd, "RequestSenderUserKey", requestSenderUserKey);
                db.AddParameter(cmd, "RequestReceiverUserKey", requestReceiverUserKey);
                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }

        public static List<UserContactRequestEnt> GetByRequestReceiverUserKeyWithPaging(string requestReceiverUserKey, long startRow, long endRow, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserContactRequestEnt> result = new List<UserContactRequestEnt>();
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByRequestReceiverUserKeyWithPaging)) {
				db.AddParameter(cmd, "RequestReceiverUserKey", requestReceiverUserKey);
				db.AddParameter(cmd, "StartRow", startRow);
				db.AddParameter(cmd, "EndRow", endRow);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
						result.Add(CreateFromReader(reader));
					}
				}
			}
			return result;
		}

        public static UserContactRequestEnt GetByRequestSenderUserKeyRequestReceiverUserKey(string requestSenderUserKey, string requestreceiverUserKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            UserContactRequestEnt result = null;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByRequestSenderUserKeyRequestReceiverUserKey)) {
                db.AddParameter(cmd, "RequestSenderUserKey", requestSenderUserKey);
                db.AddParameter(cmd, "RequestReceiverUserKey", requestreceiverUserKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = CreateFromReader(reader);
                    }
                }
            }
            return result;
        }

        public static int Insert(UserContactRequestEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
                db.AddParameter(cmd, "RequestSenderUserKey", item.RequestSenderUserKey);
				db.AddParameter(cmd, "RequestReceiverUserKey", item.RequestReceiverUserKey);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}